package main

import "fmt"
import "net"

/*
 * 判断两个 ip/mask 是否是同一个网段
 * 掩码相同且包含
 */
func ContainsCIDR(a, b *net.IPNet) bool {
	ones1, _ := a.Mask.Size()
	ones2, _ := b.Mask.Size()
	fmt.Println(ones1, ones2)
	//result1:= ones1 <= ones2 && a.Contains(b.IP)
	//result2:= ones2 <= ones1 && b.Contains(a.IP)
	//return result1 || result2
	return ones1 == ones2 && a.Contains(b.IP)
}

/*
func ContainsCIDR(a, b *net.IPNet) bool {
	ones1, _ := a.Mask.Size()
	ones2, _ := b.Mask.Size()
	fmt.Println(ones1, ones2)
	retrun ones1 <= ones2 && a.Contains(b.IP)
}
*/

func main() {
	net1 := "192.168.96.85/21"
	net2 := "192.168.102.210/21"
	_, a, err := net.ParseCIDR(net1)
	_, b, err := net.ParseCIDR(net2)
	if err != nil {
		return
	}
	result := ContainsCIDR(a,b)
	if result {
		fmt.Println(a, "and", b, "是同一网段")
	} else {
		fmt.Println(a, "and", b, "不是同一网段")
	}
}
